/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.projectdemo.web;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.foreveross.webbase.common.config.Global;
import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.web.BaseController;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.projectdemo.entity.Server;
import com.foreveross.webbase.projectdemo.entity.Servertype;
import com.foreveross.webbase.projectdemo.service.ServerService;
import com.foreveross.webbase.projectdemo.service.ServertypeService;

/**
 * 服务器管理Controller
 * @author yangwanli
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/projectdemo/server")
public class ServerController extends BaseController {

	@Autowired
	private ServerService serverService;
	
	@Autowired
	private ServertypeService servertypeService;
	@RequiresPermissions("projectdemo:server:view")
	@RequestMapping(value = {"list", ""})
	public String list(Server server) {
		Page<Server> page = serverService.findPage(new Page<Server>(request(), response()), server); 
		//获取所有服务器类型
		List<Servertype> servertypes = servertypeService.findList(new Servertype());
		attr("alltype",servertypes);
		attr("page", page);
		return "projectdemo/serverList";
	}

	@RequiresPermissions("projectdemo:server:view")
	@RequestMapping("form")
	public String form(String id) {
		if(StringUtils.isNotEmpty(id)) {
			Server server=serverService.get(id);
			attr("server", server);
		} else {
			attr("server", new Server());
		}
		attr("alltype",servertypeService.findList(new Servertype()));
		return "projectdemo/serverForm";
	}

	@RequiresPermissions("projectdemo:server:edit")
	@RequestMapping("save")
	public String save(Server server, RedirectAttributes redirectAttributes) {
		if (!beanValidator(redirectAttributes, server)){
			return form(server.getId());
		}
		serverService.save(server);
		addMessage(redirectAttributes, "保存服务器成功");
		return "redirect:"+Global.getAdminPath()+"/projectdemo/server/?repage";
	}
	
	@RequiresPermissions("projectdemo:server:edit")
	@RequestMapping("delete")
	public String delete(@RequestParam String id, RedirectAttributes redirectAttributes) {
		serverService.delete(id);
		addMessage(redirectAttributes, "删除服务器成功");
		return "redirect:"+Global.getAdminPath()+"/projectdemo/server/?repage";
	}

}